//体温监测－－－－－工作人员测量体温并上报信息
var app = getApp();
var util = require('../../../utils/util.js');
var select_fz, twvalue, inf_id;
var select_page, name, sex ,  age ,  sfz ,  phone ,  sfxiaoqu_person_car ,  car_number,  address,  out_in_mesage , 
  out_where, out_or_inv, fromwhere_time, fromwhere, memo;
var gzry_dep = "", gzry_zqdd = "", gzry_name = "", permission = false, gzryphone, fenzhenMemo, laifangmudi, beifangzhekeshi, beifangzhename, outtime,gzry_memo;
Page({
  
  data: {
    show: "",
    baseinformation: false,
    xiaoqu_view: true,
    hccc: "",
    tbry: "",
    items: [
      { name: '患者告知书', value: '患者告知书' },
      { name: '门诊分诊台', value: '门诊分诊台' },
      { name: '住院分诊台', value: '住院分诊台' },
    ],
    modalHidden: true,//是否隐藏对话框
    xiaoqu_gzrrzqdd: false,
    xiaoqu_tw_moda:true,
   
  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    this.findOpenid()
  },
  onLoad: function () {
    this.setData({
      zmyygzs_zqdd_moda: false,
    })
  },

  scan_info_return: function (e) {
    select_page ="近期返霍人员表"
    const that = this;
    if (gzry_zqdd && permission) {
      this.scanCode();
    }
    else {
      if (gzry_zqdd) {
        wx.showToast({
          title: '没有权限',
          icon: 'success'
        })
        if (gzry_name) {
          that.setData({
            tbry: "扫码功能需要管理员审核后方可使用！请联系管理人员"
          })
        }
      }
      else {
        wx.showToast({
          title: '请绑定值勤点',
          icon: 'success'
        })
      }
    }
  },

  scan_info_out: function (e) {
    select_page = "外出人员登记表"
    const that = this;
    if (gzry_zqdd && permission) {
      this.scanCode();
    }
    else {
      if (gzry_zqdd) {
        wx.showToast({
          title: '没有权限',
          icon: 'success'
        })
        if (gzry_name) {
          that.setData({
            tbry: "扫码功能需要管理员审核后方可使用！请联系管理人员"
          })
        }
      }
      else {
        wx.showToast({
          title: '请绑定值勤点',
          icon: 'success'
        })
      }
    }
  },

  scan_info_in: function (e) {
    select_page = "进入人员登记"
    const that = this;
    if (gzry_zqdd && permission) {
      this.scanCode();
    }
    else {
      if (gzry_zqdd) {
        wx.showToast({
          title: '没有权限',
          icon: 'success'
        })
        if (gzry_name) {
          that.setData({
            tbry: "扫码功能需要管理员审核后方可使用！请联系管理人员"
          })
        }
      }
      else {
        wx.showToast({
          title: '请绑定值勤点',
          icon: 'success'
        })
      }
    }
  },

 
  findOpenid: function () {
    var that = this;
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        // console.log('onGetOpenid result: ', app.globalData.openid)
        app.globalData.openid = res.result.openid

        db.collection('glry').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              // wx.showToast({
              //   title: '已添加过'
              // })
              // console.log('已添加过', res.data)
              // console.log('已添加过', res.data[0]._id)
              this.setData({
                tbry: "填报人员为 " + res.data[0].dep + " 的: " + res.data[0].name,
                gzry_zqdd: res.data[0].zqaddress,
                gzry_name: res.data[0].name,
                // gzry_name: res.data[0].姓名,
                // name_phone: res.data[0].电话,               
              })
              // personid = res.data[0]._id
              gzry_dep = res.data[0].dep
              gzry_zqdd = res.data[0].zqaddress
              gzry_name = res.data[0].name
              gzryphone = res.data[0].gzryphone
              permission = res.data[0].ruler
              // name_phone = res.data[0].电话
            }
            else {
              // console.log('没e有人员信息', res.data)
              // console.log('this.data.modalHidden1', this.data.modalHidden1)
              this.setData({
                tbry: "请到 服务中心-->工作人员绑定 后再使用扫码功能！"
              })
            }
          })
      }
    })


  },
  //事件处理函数
  // bindViewTap: function () {
  //   this.setData({
  //     modalHidden: !this.data.modalHidden
  //   })

  // },
  
  
  //确定按钮点击事件,确认值勤信息
  xiaoqu_zqdd_en: function () {
    this.setData({
      xiaoqu_gzrrzqdd: true
    })
  },
  //取消按钮点击事件,确认值勤信息
  xiaoqu_zqdd_ca: function () {
    wx.navigateTo({
      url: '/pages/gzryinfo/gzryinfo'
    })
    this.setData({
      xiaoqu_gzrrzqdd: true
    })
  },
  //确定按钮点击事件,体温
xiaoqu_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.add_xiaoqu();
    this.setData({
      xiaoqu_tw_moda: true
    })
  },
  //取消按钮点击事件,体温
 xiaoqu_tw_ca: function () {
    // console.log('取消按钮点击事件,门诊体温：') 
    // wx.navigateTo({
    //   url: '/pages/gzryinfo/gzryinfo'
    // })
    this.setData({
      xiaoqu_tw_moda: true
    })
  },
  test: function () {
    // console.log("二维码成功222：", name1, sex1, sfz1)
  },
  scanCode: function (e) {

    const db = wx.cloud.database();
    wx.scanCode({
      // onlyFromCamera: true,////只允许从相机扫码，去掉可加载相册图片
      success: (res) => {
        // this.show = "结果:" + res.result + "二维码类型:" + res.scanType + "字符集:" + res.charSet + "路径:" + res.path;
        // app.globalData.TwoCode = false
        inf_id = res.result
        db.collection('base').doc(res.result).get(
          // console.log("二维码成功,id：", res.result)
        ).then(res => {
          // res.data 包含该记录的数据
          // console.log("二维码成功,携带数据：", res.data)
          this.setData({
            returename: "上次复核的人员为：" + res.data.name,//提交后提示上次填加人员
            xiaoqu_view:false,
            baseinformation: true,
            name: res.data.name,
            sex: res.data.sex,
            age: res.data.age,
            sfz: res.data.sfz,
            phone: res.data.phone,
            sfxiaoqu_person_car: res.data.sfxiaoqu_person_car,
            car_number: res.data.car_number,
            address: res.data.address,
            out_in_mesage: res.data.out_in_mesage,
            out_where: res.data.out_where,
            out_or_inv: res.data.out_or_inv,
            fromwhere_time: res.data.fromwhere_time,
            fromwhere: res.data.fromwhere,
            memo: res.data.memo


          })
            
            name=  res.data.name,
            sex=  res.data.sex,
            age=  res.data.age,
            sfz=  res.data.sfz,
            phone=  res.data.phone,
            sfxiaoqu_person_car=  res.data.sfxiaoqu_person_car,
            car_number=  res.data.car_number,
            address=  res.data.address,
            out_in_mesage=  res.data.out_in_mesage,
            out_where=  res.data.out_where,
            out_or_inv=  res.data.out_or_inv,
            fromwhere_time=  res.data.fromwhere_time,
            fromwhere=  res.data.fromwhere,
            memo=  res.data.memo
        
          // // console.log("门诊台选择2：", select_fz)
         


        })
          .catch(Error => {
            // console.log("二维码失败wyth：")
            wx.showToast({
              title: '无效信息码',
              icon: 'success',
              duration: 2000
            })
          })



      },
      fail: (res) => {
        // console.log("二维码扫描结果5：", res)
        wx.showToast({
          title: '失败',
          icon: 'success',
          duration: 2000
        })
      },
      complete: (res) => {
        // console.log("二维码扫描失败：", TwoCode )
      }

    })



  },
  
  
 xiaoqu_twvalue_check: function (e) {
    if (parseFloat(twvalue) > 34 && parseFloat(twvalue) < 41) {
      // console.log("输出1：", parseFloat(twvalue))
      if (parseFloat(twvalue) >= 37.3) {
        // console.log("wv分诊选择", select_fz)
        
            this.setData({
              xiaoqu_tw_moda: false
              // inputVal: "",
            })
         
      }
      else {
        // console.log("体温正常", parseFloat(twvalue))
        this.add_xiaoqu();

      }
    }
    else {
      // console.log("不在范围", parseFloat(twvalue))
      wx.showToast({
        title: '体温不对请重测',
        icon: 'success'
      })
    }
  },
  
  add_xiaoqu: function (e) {
    const db = wx.cloud.database()
    this.setData({
      xiaoqu_view: true,
      baseinformation: false,   ////扫描页面
      inputVal: "",     //体温 输入 框
      gzry_memo: "",
    })
    // console.log("yn康记录ID1111111", gzry_memo)
    db.collection('info').add({
      // data 传入需要局部更新的数据
     
      data: {

        // 表示将 done 字段置为 true 
        name: name,
        sex: sex,
        age: age,
        sfz: sfz,
        phone: phone,
        sfxiaoqu_person_car: sfxiaoqu_person_car,
        car_number: car_number,
        address: address,
        out_in_mesage: out_in_mesage,
        out_where: out_where,
        out_or_inv: out_or_inv,
        fromwhere_time: fromwhere_time,
        fromwhere: fromwhere,
        memo: memo,
        form: "小区测温",
        gzryphone: gzryphone,
        gzry_addtime: util.formatTime(new Date()),
        gz_address: gzry_zqdd,
        gzry_dep: gzry_dep,
        gzry_name: gzry_name,
        twvalue: twvalue,
        gzry_memo :gzry_memo,
        select_page: select_page,
        outtime:""
       
      },
      success: function (res) {        
        
        console.log("yn康记录I33333333333333", gzry_memo)
        wx.showToast({
          title: '人员上报成功',
          icon: 'success',
        })
      }
    })
    twvalue = ""
    gzry_memo = ""

  }
  ,


  onInputEvent: function (e) {
    twvalue = e.detail.value
    this.setData({
      returetwvalue: "  体温：" + twvalue
    })
  },
  memo_Event: function (e) {
    gzry_memo = e.detail.value
    // console.log("yn康记录ID", gzry_memo)
    // this.setData({
    //   gzry_memo: "  体温：" + twvalue
    // })
  },



})
